﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
 
 

namespace SHKF.B2C.Mannage.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class UserController : BaseController
    {
       
        public UserController(
            ILogger<UserController> logger) : base(logger)
        {
            
        }

        [HttpGet("list")]
        public DataDto<List<UserEntity>> List(int CompanySN, int ShopSN, string UserName)
        { 
            DataDto<List<UserEntity>> dto = new DataDto<List<UserEntity>>();
            try
            {
                //dto.data = this.userBLL.Query(CompanySN, ShopSN, UserName); 
            }
            catch (Exception ex)
            {
                dto = new DataDto<List<UserEntity>>();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }

            return dto;
        }

        [HttpPost("Save")]
        public DataDto<UserEntity> Save(UserEntity user)
        {
            DataDto<UserEntity> dataDto = new DataDto<UserEntity>();
            int code = 0;
            string msg = "";

            try
            {
                //如果添加的时候，已经存在用户名
                //if (user.SN == 0 && this.userBLL.IsExist(user.UserName))
                //{
                //    code = -1;
                //    msg = "用户名已经存在";
                //}
                //else
                //{
                //    UserEntity userEntity = this.userBLL.Save(user);
                //    dataDto.data = userEntity;
                //}
            }
            catch (Exception ex)
            {
                code = ex.HResult;
                msg = ex.Message;
            }

            if (code != 0)
            {
                Error error = new Error();
                error.code = code;
                error.msg = msg;
                dataDto.error = error;
            }

            return dataDto;
        }

        [HttpGet("delete")]
        public DataDto<int> Delete(int SN)
        {
            DataDto<int> dataDto = new DataDto<int>();
            int code = 0;
            string msg = "";

            try
            {
                //如果添加的时候，已经存在用户名
               // dataDto.data = this.userBLL.Delete(SN);
            }
            catch (Exception ex)
            {
                code = ex.HResult;
                msg = ex.Message;
            }

            if (code != 0)
            {
                Error error = new Error();
                error.code = code;
                error.msg = msg;
                dataDto.error = error;
            }

            return dataDto;
        }

        [HttpGet("Name")]
        public JsonResult GetUserName()
        { 
            DataDto<string> user = new DataDto<string>();
            user.data = CreateUserName();
             
            //user.error = new Error() { msg =  "year:" + str1 + " Day:" + str2 + " Millisecond:" + now.Millisecond.ToString() + " now:" + now.ToString("yyyy-MM-dd HH:mm:ss")};
            return new JsonResult(user);
        } 
        private string CreateUserName()
        {
            DateTime now = DateTime.Now;

            string strMonth = now.Month.ToString().PadLeft(2, '0');
            string strDay = now.Day.ToString().PadLeft(2, '0');
            string str1 = $"{now.Year}{strMonth}{strDay}";

            string strHour = now.Hour.ToString().PadLeft(2, '0');
            string strMinute = now.Minute.ToString().PadLeft(2, '0');
            string strSecond = now.Second.ToString().PadLeft(2, '0');
            string str2 = $"{strHour}{strMinute}{strSecond}";

            int iYear = int.Parse(str1);
            int iDay = int.Parse(str2);

            string strMill = now.Millisecond.ToString().PadLeft(3, '0');
            string userName = (iYear + iDay).ToString() + strMill;
            return userName;
        }

    }
}
